package com.quanjiaolian.tools;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;

import org.apache.log4j.Logger;

import com.cloopen.rest.sdk.CCPRestSmsSDK;

public class SendTemplateSMS {
	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(SendTemplateSMS.class.getName());
	/**
	 * 声明短信服务器需要的变量
	 */
	private static String SMS_Url;//短信服务器URL地址
	private static String SMS_Port;//短信服务器端口
	private static String SMS_Sid;//短信服务端SID
	private static String SMS_Token;//短信服务端Token
	private static String SMS_Appid;//短信服务端Appid
	private static String SMS_SMStemplateId;//短信模版ID
	
	 /**
	 * 加载配置文件
	 * 
	 * @see Servlet#init(ServletConfig)
	 */

	public static void init() {
		// 读取配置文件
		try {
			// 读取文件
			resourceBundle = ResourceBundle.getBundle("SMSConfig");
			SMS_Url = resourceBundle.getString("sms.url");
			SMS_Port = resourceBundle.getString("sms.port");
			SMS_Sid = resourceBundle.getString("sms.sid");
			SMS_Token = resourceBundle.getString("sms.token");
			SMS_Appid = resourceBundle.getString("sms.appid");
			SMS_SMStemplateId = resourceBundle.getString("sms.SMStemplateId");
			logger.info("*****************************init SMS Server connection info******************");
			logger.info("Loding SendTemplateSMS properties success!");
		} catch (Exception e) {
			logger.info("Loading SendTemplateSMS properties error!  " + e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String,Object> sendSMS(String msisdn){
		init();
		logger.info("SMS_Url is: "+SMS_Url);
		logger.info("SMS_Port is: "+SMS_Port);
		logger.info("SMS_Sid is: "+SMS_Sid);
		logger.info("SMS_Token is: "+SMS_Token);
		logger.info("SMS_Appid is: "+SMS_Appid);
		logger.info("SMS_SMStemplateId is: "+SMS_SMStemplateId);
		logger.info("***************************************************************************");
		
		HashMap<String, Object> result = null;
		Map<String,Object> map = new HashMap<String, Object>();
		String code = Utils.makeCode();//生成短信验证码
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(SMS_Url,SMS_Port);// 初始化服务器地址和端口

		restAPI.setAccount(SMS_Sid,SMS_Token);// 初始化主账号名称和主账号令牌
		restAPI.setAppId(SMS_Appid);// 初始化应用ID
		result = restAPI.sendTemplateSMS(msisdn,SMS_SMStemplateId,new String[]{code,"10"});//传入需要发送的用户手机号码、模版ID和信息
		logger.info("SDKTestGetSubAccounts result=" + result);
		if ("000000".equals(result.get("statusCode"))) {
			// 正常返回输出data包体信息（map）
			HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
					
			Set<String> keySet = data.keySet();
			for (String key : keySet) {
				Object object = data.get(key);
				logger.info(key + " = " + object);
			}
			map.put("msg","Send SMS OK");
			map.put("code",code);
			map.put("msisdn",msisdn);
			logger.info("短信发送成功!已向手机号为："+msisdn+"的用户发送验证码："+code);
		} else {
			// 异常返回输出错误码和错误信息
			logger.info("错误码=" + result.get("statusCode") + " 错误信息= "+result.get("statusMsg"));
			map.put("msg","Send SMS Error");
			map.put("code",result.get("statusCode"));
			map.put("msisdn",msisdn);
		}
		
		return map;
	}
	
	public static void main(String[] args) {
//		String str = sendSMS("15101032667",new String[]{"3306","10"});
//		logger.info("SMS result is:"+str);
	}


}
